<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="80%" destroy-on-close @closed="$emit('closed')" align-center>
        <el-header>
            <div class="left-panel">
                <div class="right-panel-search">
                    <div class="search-input">物料名称</div>
                    <!-- <el-input v-model="search.material_name" placeholder="请输入物料名称" clearable @change="handleKeyDown" /> -->
                    <el-select v-model="search.material_id"  filterable placeholder="请选择物料名称" clearable>
                        <el-option v-for="item in materialList" :key="item.id" :label="item.material_name" :value="item.id"/>
                    </el-select>
                    <div class="search-input">供应商</div>
                    <el-select v-model="search.supplier_id"  filterable placeholder="请选择供应商" clearable>
                        <el-option v-for="item in applyList" :key="item.apply_id" :label="item.apply_name" :value="item.apply_id"/>
                    </el-select>
                    <div class="search-input">skc</div>
                    <el-input v-model="search.skc" placeholder="请输入skc" clearable @change="handleKeyDown" />
                    <div class="search-input">色号</div>
                    <el-input v-model="search.color_number" placeholder="请输入色号" clearable @change="handleKeyDown" />
                    <div class="search-input">颜色</div>
                    <el-input v-model="search.colour" placeholder="请输入颜色" clearable @change="handleKeyDown" />
                    <el-button type="primary" icon="el-icon-search" @click="upsearch">搜索</el-button>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" :apiObj="apiObj" @select-all="selectAll" @selection-change="selectionChange" stripe remoteSort remoteFilter border height='500' :params="query">
                <el-table-column type="selection" width="50"></el-table-column>
                <el-table-column type="index" width="50"></el-table-column>
                <el-table-column label="物料类别" align="center" width="100">
                    <template #default="scope">
                        <div v-if="scope.row.material">{{scope.row.material.category_name}}</div>
                    </template>
                </el-table-column>
                <el-table-column label="skc" prop="skc" align="center" width="210"></el-table-column>
                <el-table-column label="物料名称" align="center">
                    <template #default="scope">
                        <div v-if="scope.row.material">{{scope.row.material.material_name}}</div>
                    </template>
                </el-table-column>
                <el-table-column label="色号" prop="color_number" align="center"></el-table-column>
                <el-table-column label="颜色" prop="colour" align="center"></el-table-column>
                <el-table-column label="图片" prop="image" align="center">
                    <template #default="scope">
                        <el-image style="width:50px;height:50px" :src="scope.row.image" :preview-src-list="scope.row.image_arr" :initial-index="0" fit="cover" preview-teleported="true" hide-on-click-modal />
                    </template>
                </el-table-column>
                <el-table-column label="单位" align="center">
                    <template #default="scope">
                        <div v-if="scope.row.material">{{scope.row.material.unit_name}}</div>
                    </template>
                </el-table-column>
                <!-- <el-table-column label="版本" prop="factory_quote" align="center"></el-table-column> -->
                <el-table-column label="供应商" align="center">
                    <template #default="scope">
                        <div v-if="scope.row.material">{{scope.row.material.apply_name}}</div>
                    </template>
                </el-table-column>
                <el-table-column label="备注" prop="remark" align="center"></el-table-column>
            </scTable>
        </el-main>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" @click="back" :disabled="selection.length == 0">选择</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
    props:{
        query: { type: String, default: "" },
    },
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '选择物料',
                edit: '选择物料',
			},
			visible: false,
			isSaveing: false,
            apiObj: this.$API.develop.edition.query,
            // query:{
            //     category_id:this.wuIndex
            // },
            selection: [],
            wuIndex:"",
            search:{
                skc:null,
                colour:null,
                color_number:null,
                material_id:null,
                supplier_id:null,
            },
            tableData:[],
            applyList:[],
            materialList:[],
            item:{},
            index:"",
            row:{},
            index1:"",
		}
	},
	mounted() {
        this.getApply()
        this.getMaterial()
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
        selectAll() {
            if(this.mode=='add'){
                if(this.query.category_id == 1){
                    this.$message.error("请选择一条数据！")
                    this.$refs.table.clearSelection()
                }
            }
        },
        selectionChange(selection) {
            if(this.mode=='add'){
                if(this.query.category_id ==1){
                    if(selection.length > 1){
                        const del_row = selection.shift()
                        this.$refs.table.toggleRowSelection(del_row,false) //设置这一行取消选中
                    }else{
                        this.selection = selection;
                        console.log('this.selection :>> ', this.selection);
                    }
                }else{
                    this.selection = selection;
                }
            }else{
                this.selection = selection;
            }
		},
        back(){
            this.$emit('success', this.selection, this.query.category_id,this.index,this.row,this.item,this.index1)
            this.visible = false;
        },
        async setData(data) {
            this.item = data.item;
            this.index = data.index;
            this.row = data.row;
            this.index1 = data.index1;
		},
        //搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
        handleKeyDown() {
            this.upsearch()
        },
        // 获取供应商
        async getApply(){
            let e = this.query.category_id
            var res = await this.$API.all.Apply.get({type:e==1?3:4});
            this.applyList = res.data;
        },
        // 获取物料名称
        async getMaterial(){
            var res = await this.$API.all.material.get({category_id:this.query.category_id});
            this.materialList = res.data;
        },
	}
}
</script>

<style></style>
